<?php
	/*********************************/
	/**** Fonctions AVAILABILITY *****/
	/*********************************/

	function getAllAvailabilityFromUser($id_user){
		$sql = 'SELECT availability.id_availability, availability.date_begin, availability.duration, availability.daily, availability.weekly, availability.monthly, availability.yearly, availability.price, DAYNAME(availability.date_begin) as dayweek, DAY(availability.date_begin) as daymonth, DATE_FORMAT(availability.date_begin, "%d/%m") as dayyear, availability.active FROM availability
				WHERE id_user = '.mysql_real_escape_string($id_user).' 
				AND( NOT EXISTS (SELECT time.date FROM time WHERE time.date = availability.date_begin))
				AND availability.active != 0 ';
		return fetch($sql);
	}

	function setAvailabilityToUser($id_user, $date_begin, $duration, $recurrence, $price){
		$daily = 0;
		$monthly = 0;
		$weekly = 0;
		$yearly = 0;
		if($recurrence == "daily"){
			$daily = 1;
		}
		else if($recurrence == "weekly"){
			$weekly = 1;
		}
		else if($recurrence == "monthly"){
			$monthly = 1;
		}
		else if($recurrence == "yearly"){
			$yearly = 1;
		}
		$sql = 'INSERT INTO availability (id_user, date_begin, duration, daily, weekly, monthly, yearly, active, price) 
		VALUES('.$id_user.',"'.$date_begin.'", "'.$duration.'", '.$daily.', '.$weekly.','.$monthly.', '.$yearly.', 1, '.$price.') ';

		return mysql_query($sql);
	}

	function getAvailabilities($l){
		$limit = (int)$l;
		$sql = 'SELECT availability.id_availability, user.id_user, user.firstname, user.lastname, availability.date_begin, availability.duration, availability.daily, availability.weekly, availability.monthly, availability.yearly, availability.price
		FROM user
		INNER JOIN availability ON availability.id_user = user.id_user
		WHERE ( NOT EXISTS (SELECT time.date FROM time WHERE time.date = availability.date_begin))
		AND availability.active != 0
		ORDER BY availability.date_begin DESC ';
		if($limit){
			$sql .= ' LIMIT '.$limit;
		}
		return fetch($sql);
	}

	function getAllAvailabilityLimited($datetime, $limit ,$offset){ 	/* faire un offset++ a chaque fois qu'on apuuye sur suivant*/ 
		$A = $offset*$limit;
		$sql = 'SELECT availability.id_availability, availability.date_begin, availability.duration, availability.daily, availability.weekly, availability.monthly, availability.yearly, availability.price FROM availability
		WHERE availability.date_begin <= "'.$datetime.'" 
		AND (availability.date_begin = "'.$datetime.'"
			OR availability.daily = 1
			OR (DAYNAME(availability.date_begin) = DAYNAME("'.$datetime.'") AND availability.weekly = 1)
			OR (DAYOFMONTH(availability.date_begin) = DAYOFMONTH("'.$datetime.'") AND availability.monthly = 1)
			OR (DAYOFMONTH(availability.date_begin) = DAYOFMONTH("'.$datetime.'") AND availability.yearly = 1))
		AND ( NOT EXISTS (SELECT time.date FROM time WHERE time.date = availability.date_begin))
		AND availability.active != 0
		LIMIT '.$A.', '.$limit.'';
		return fetch($sql);
	}

	function getAVailabilityPriceById($id_availability){
		$sql = 'SELECT availability.price FROM availability
		WHERE availability.id_availability = '.$id_availability.'';
		return fetch($sql);
	}

	function updateAvailabilityActive($id_availability){
		$sql = 'UPDATE `availability` SET `active` = 0 WHERE availability.id_availability = '.$id_availability.'';
		return mysql_query($sql);
	}
	?>